#include <stdlib.h>
#include <string.h>
#include <device/screen.h>
#include "graphic.h"

/*
 * check that point (x,y) is in rectangular r
 */
EXPORT int in_rect( int x, int y, RECT *r ) {
    return r -> c.left <= x && x < r -> c.right
        && r -> c.top <= y && y < r -> c.bottom;
}

/*
 * draw one pixel
 *  draw one pixel at position (x,y) with color c
 */
EXPORT void draw_pixel( int x, int y, int c, BMP *p_bmp )
{
    switch (p_bmp -> pixbits >> 8) {
    case 8 :
        ((UB*)((p_bmp -> baseaddr)[0]))[p_bmp -> rowbytes * y + x] = c;
        break;
    case 16 :
        c = colormap[c]; /* color map number -> BMP value */
        c = ((c >> 16) & 0xff) >> 3 << 11 /* red */
          | ((c >> 8) & 0xff) >> 2 << 5   /* edge */
          | (c & 0xff) >> 3;              /* blue */
        ((UH*)((p_bmp -> baseaddr)[0]))[p_bmp -> rowbytes / 2 * y + x] = c;
        break;
    case 32 :
        c = colormap[c]; /* color map number -> BMP value */
        ((UW*)((p_bmp -> baseaddr)[0]))[p_bmp -> rowbytes / 4 * y + x] = c;
        break;
    }
}

/*
 * draw line
 *  (x1,y1) to (x2,y2) draw line with color c
 */
EXPORT void draw_line( int x1, int y1, int x2, int y2, int c, BMP *p_bmp )
{
    int t, u, a, b, z;

    if (!in_rect( x1, y1, &(p_bmp -> bounds)) ||
        !in_rect( x2, y2, &(p_bmp -> bounds) )) {

    } else if ((a = abs( x2 - x1 )) > (b = abs( y2 - y1 ))) {
        if (x1 > x2) {
            t = x1; x1 = x2; x2 = t;
            t = y1; y1 = y2; y2 = t;
        }
        u = y1; z = a >> 1;
        if (y1 < y2) {
            for (t = x1; t <= x2; t++) {
                draw_pixel( t, u, c, p_bmp );
                z += b;
                if (z > a) { u++; z -= a; }
            }
        } else {
            for (t = x1; t <= x2; t++) {
                draw_pixel( t, u, c, p_bmp );
                z += b;
                if (z > a) { u--; z -= a; }
            }
        }
    } else if (y2 != y1) {
        if (y1 > y2) {
            t = x1; x1 = x2; x2 = t;
            t = y1; y1 = y2; y2 = t;
        }
        u = x1; z = b >> 1;
        if (x1 < x2) {
            for (t = y1; t <= y2; t++) {
                draw_pixel( u, t, c, p_bmp );
                z += a;
                if (z > b) { u++; z -= b; }
            }
        } else {
            for (t = y1; t <= y2; t++) {
                draw_pixel( u, t, c, p_bmp );
                z += a;
                if (z > b) { u--; z -= b; }
            }
        }
    } else {
        draw_pixel( x1, y1, c, p_bmp );
    }
}

/*
 * fill rectangle
 *  fill rectangle (x1,y1)to(x2-1,y2-1) with color c
 */
EXPORT void fill_rect( int x1, int y1, int x2, int y2, int c, BMP *p_bmp )
{
    int u, v;
    if (x1 <= x2 && y1 <= y2 && in_rect( x1, y1, &(p_bmp -> bounds) )
        && in_rect( x2 - 1, y2 - 1, &(p_bmp -> bounds) )) {
        for(v = y1; v < y2; v++) {
            for(u = x1; u < x2; u++) {
                draw_pixel( u, v, c, p_bmp );
            }
        }
    }
}

/*
 * bitmap image of characters (8 x 16 pixels)
 */
LOCAL UB img[94][16] = {
 {0x00,0x10,0x38,0x38,0x38,0x38,0x38,0x10,0x10,0x10,0x10,0x00,0x10,0x38,0x10,0x00} /* ! */
,{0x00,0x28,0x28,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /* " */
,{0x00,0x00,0x28,0x28,0x2e,0x38,0xe8,0x28,0x2e,0x38,0xe8,0x28,0x28,0x28,0x00,0x00} /* # */
,{0x10,0x10,0x38,0x54,0x92,0x92,0x50,0x30,0x18,0x14,0x92,0x92,0x54,0x38,0x10,0x10} /* $ */
,{0x00,0x42,0xa2,0xa4,0xa4,0xa8,0x48,0x10,0x10,0x24,0x2a,0x4a,0x4a,0x8a,0x84,0x00} /* % */
,{0x00,0x38,0x44,0x44,0x44,0x44,0x28,0x10,0x2a,0x4a,0x44,0x84,0x84,0x8a,0x72,0x00} /* & */
,{0x00,0x20,0x20,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /* ' */
,{0x02,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x08,0x08,0x04,0x02} /* ( */
,{0x80,0x40,0x20,0x20,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x20,0x20,0x40,0x80} /* ) */
,{0x00,0x00,0x00,0x10,0x10,0x92,0x54,0x38,0x54,0x92,0x10,0x10,0x00,0x00,0x00,0x00} /* * */
,{0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x7c,0x10,0x10,0x10,0x10,0x00,0x00,0x00,0x00} /* + */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x20,0x20,0x40} /* , */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /* - */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00} /* . */
,{0x00,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x40,0x80,0x80,0x00} /* / */
,{0x00,0x38,0x44,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x44,0x38,0x00} /* 0 */
,{0x00,0x10,0x30,0x50,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7c,0x00} /* 1 */
,{0x00,0x38,0x44,0x82,0x82,0x02,0x04,0x04,0x08,0x10,0x10,0x20,0x42,0x82,0xfe,0x00} /* 2 */
,{0x00,0x38,0x44,0x82,0x02,0x02,0x04,0x38,0x04,0x02,0x02,0x02,0x82,0x44,0x38,0x00} /* 3 */
,{0x00,0x0c,0x0c,0x14,0x14,0x24,0x24,0x44,0x44,0x84,0x84,0xfe,0x04,0x04,0x04,0x00} /* 4 */
,{0x00,0xfc,0x80,0x80,0x80,0x80,0xb8,0xc4,0x02,0x02,0x02,0x02,0x02,0x84,0x78,0x00} /* 5 */
,{0x00,0x3c,0x42,0x80,0x80,0x80,0xb8,0xc4,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00} /* 6 */
,{0x00,0xfe,0x82,0x82,0x84,0x04,0x04,0x08,0x08,0x08,0x08,0x10,0x10,0x10,0x10,0x00} /* 7 */
,{0x00,0x38,0x44,0x82,0x82,0x82,0x44,0x38,0x44,0x82,0x82,0x82,0x82,0x44,0x38,0x00} /* 8 */
,{0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x46,0x3a,0x02,0x02,0x02,0x84,0x78,0x00} /* 9 */
,{0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00} /* : */
,{0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x30,0x30,0x10,0x10,0x20} /* ; */
,{0x00,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x00} /* < */
,{0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00} /* = */
,{0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00} /* > */
,{0x00,0x38,0x44,0x82,0x82,0x02,0x04,0x08,0x10,0x10,0x10,0x00,0x00,0x10,0x10,0x00} /* ? */
,{0x00,0x38,0x44,0x82,0x9a,0xaa,0xaa,0xaa,0xaa,0xaa,0xac,0x90,0x80,0x42,0x3c,0x00} /* @ */
,{0x00,0x10,0x10,0x28,0x28,0x28,0x44,0x44,0x44,0x7c,0x82,0x82,0x82,0x82,0x82,0x00} /* A */
,{0x00,0xf8,0x84,0x82,0x82,0x82,0x84,0xf8,0x84,0x82,0x82,0x82,0x82,0x84,0xf8,0x00} /* B */
,{0x00,0x38,0x44,0x82,0x82,0x80,0x80,0x80,0x80,0x80,0x80,0x82,0x82,0x44,0x38,0x00} /* C */
,{0x00,0xf8,0x84,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x84,0xf8,0x00} /* D */
,{0x00,0xfe,0x80,0x80,0x80,0x80,0x80,0xfc,0x80,0x80,0x80,0x80,0x80,0x80,0xfe,0x00} /* E */
,{0x00,0xfe,0x80,0x80,0x80,0x80,0x80,0xfc,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00} /* F */
,{0x00,0x38,0x44,0x82,0x82,0x80,0x80,0x80,0x8e,0x82,0x82,0x82,0x82,0x44,0x38,0x00} /* G */
,{0x00,0x82,0x82,0x82,0x82,0x82,0x82,0xfe,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x00} /* H */
,{0x00,0x7c,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7c,0x00} /* I */
,{0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x82,0x84,0x78,0x00} /* J */
,{0x00,0x82,0x82,0x84,0x88,0x90,0xa0,0xd0,0x90,0x88,0x88,0x84,0x84,0x82,0x82,0x00} /* K */
,{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xfe,0x00} /* L */
,{0x00,0x82,0x82,0xc6,0xc6,0xaa,0xaa,0xaa,0x92,0x92,0x92,0x82,0x82,0x82,0x82,0x00} /* M */
,{0x00,0x82,0x82,0xc2,0xc2,0xa2,0xa2,0x92,0x92,0x8a,0x8a,0x86,0x86,0x82,0x82,0x00} /* N */
,{0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00} /* O */
,{0x00,0xf8,0x84,0x82,0x82,0x82,0x82,0x84,0xf8,0x80,0x80,0x80,0x80,0x80,0x80,0x00} /* P */
,{0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xba,0xc6,0x44,0x3a,0x00} /* Q */
,{0x00,0xf8,0x84,0x82,0x82,0x82,0x84,0xf8,0x88,0x88,0x84,0x84,0x84,0x82,0x82,0x00} /* R */
,{0x00,0x38,0x44,0x82,0x82,0x80,0x40,0x30,0x0c,0x02,0x02,0x82,0x82,0x44,0x38,0x00} /* S */
,{0x00,0xfe,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x00} /* T */
,{0x00,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00} /* U */
,{0x00,0x82,0x82,0x82,0x82,0x82,0x44,0x44,0x44,0x44,0x28,0x28,0x28,0x10,0x10,0x00} /* V */
,{0x00,0x82,0x82,0x92,0x92,0x92,0x92,0xaa,0xaa,0xaa,0xaa,0x44,0x44,0x44,0x44,0x00} /* W */
,{0x00,0x82,0x82,0x44,0x44,0x28,0x28,0x10,0x10,0x28,0x28,0x44,0x44,0x82,0x82,0x00} /* X */
,{0x00,0x82,0x82,0x82,0x44,0x44,0x44,0x28,0x28,0x10,0x10,0x10,0x10,0x10,0x10,0x00} /* Y */
,{0x00,0xfe,0x82,0x84,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x42,0x82,0xfe,0x00} /* Z */
,{0x00,0x1e,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1e,0x00} /* [ */
,{0x00,0x82,0x82,0x44,0x44,0x28,0x28,0xfe,0x10,0x10,0xfe,0x10,0x10,0x10,0x10,0x00} /* \ */
,{0x00,0x78,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x78,0x00} /* ] */
,{0x18,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /* ^ */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe} /* _ */
,{0x30,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /* ` */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* a */
,{0x00,0x80,0x80,0x80,0x80,0x80,0xb8,0xc4,0x82,0x82,0x82,0x82,0x82,0xc4,0xb8,0x00} /* b */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x82,0x80,0x80,0x80,0x80,0x42,0x3c,0x00} /* c */
,{0x00,0x02,0x02,0x02,0x02,0x02,0x3a,0x46,0x82,0x82,0x82,0x82,0x82,0x46,0x3a,0x00} /* d */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x44,0x82,0xfe,0x80,0x80,0x80,0x42,0x3c,0x00} /* e */
,{0x00,0x1c,0x22,0x20,0x20,0x20,0xfc,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00} /* f */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x7a,0x84,0x84,0x78,0x40,0x80,0x7c,0x82,0x82,0x7c} /* g */
,{0x00,0x80,0x80,0x80,0x80,0x80,0xb8,0xc4,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x00} /* h */
,{0x00,0x10,0x10,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x00} /* i */
,{0x00,0x04,0x04,0x00,0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x84,0x78} /* j */
,{0x00,0x80,0x80,0x80,0x80,0x80,0x84,0x88,0x90,0xa0,0xe0,0x90,0x88,0x84,0x82,0x00} /* k */
,{0x00,0x30,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x38,0x00} /* l */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x6c,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x00} /* m */
,{0x00,0x00,0x00,0x00,0x00,0x00,0xb8,0xc4,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x00} /* n */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00} /* o */
,{0x00,0x00,0x00,0x00,0x00,0x00,0xb8,0xc4,0x82,0x82,0x82,0x82,0xc4,0xb8,0x80,0x80} /* p */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x3a,0x46,0x82,0x82,0x82,0x82,0x46,0x3a,0x02,0x02} /* q */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x98,0xa4,0xc0,0x80,0x80,0x80,0x80,0x80,0x80,0x00} /* r */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x7c,0x82,0x82,0x60,0x1c,0x02,0x82,0x82,0x7c,0x00} /* s */
,{0x00,0x00,0x20,0x20,0x20,0x20,0xfc,0x20,0x20,0x20,0x20,0x20,0x20,0x22,0x1c,0x00} /* t */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x46,0x3a,0x00} /* u */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x82,0x82,0x82,0x44,0x44,0x28,0x28,0x10,0x10,0x00} /* v */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x82,0x92,0x92,0x92,0xaa,0xaa,0xaa,0x44,0x44,0x00} /* w */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x82,0x82,0x44,0x28,0x10,0x28,0x44,0x82,0x82,0x00} /* x */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x82,0x82,0x44,0x44,0x28,0x28,0x10,0x10,0x20,0xc0} /* y */
,{0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0xfe,0x00} /* z */
,{0x00,0x04,0x08,0x08,0x08,0x08,0x08,0x10,0x08,0x08,0x08,0x08,0x08,0x08,0x04,0x00} /* { */
,{0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x00} /* | */
,{0x00,0x40,0x20,0x20,0x20,0x20,0x20,0x10,0x20,0x20,0x20,0x20,0x20,0x20,0x40,0x00} /* } */
,{0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /* ~ */
};
LOCAL UB img_vn[17][16] = {
 {0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* as */
,{0x00,0x00,0x40,0x20,0x10,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* af */
,{0x00,0x38,0x08,0x08,0x10,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* ar */
,{0x00,0x00,0x32,0x4c,0x00,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* ax */
,{0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x10} /* aj */
,{0x00,0x00,0x10,0x28,0x44,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aa */
,{0x00,0x01,0x12,0x2c,0x44,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aas */
,{0x00,0x80,0x08,0x68,0x44,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aaf */
,{0x0e,0x02,0x02,0x2c,0x44,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aar */
,{0x32,0x4c,0x04,0x18,0x44,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aax */
,{0x00,0x00,0x00,0x10,0x44,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x10} /* aaj */
,{0x00,0x00,0x00,0x48,0x30,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aw */
,{0x04,0x08,0x10,0x48,0x30,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* aws */
,{0x40,0x20,0x10,0x48,0x30,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* awf */
,{0x18,0x08,0x10,0x48,0x30,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* awr */
,{0x32,0x4c,0x00,0x48,0x30,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x00} /* awx */
,{0x00,0x00,0x00,0x48,0x30,0x00,0x78,0x84,0x04,0x1c,0x64,0x84,0x84,0x8c,0x72,0x10} /* awj */
};
//the same for the others

/*
 * draw string
 *  draw string str at (x,y) with color c
 */
EXPORT void draw_text( char *str, int x, int y, int c, BMP *p_bmp )
{
    int k, h, b, m;
    for(; *str != '\0'; str++) {
        k = *str - '!';
        if (k < 0 || k >= 94) {
            x += 8; continue;
        }
        for(h = 0; h < 16; h++) {
            b = img[k][h];
            for(m = 7; m >= 0; m--) {
                if (b & 1) {
                    draw_pixel( x + m, y + h, c, p_bmp );
                }
                b >>= 1;
            }
        }
        x += 8;
    }
}
void draw_char_vn(int pos,int x, int y, int c, BMP *p_bmp )
{
	int h,b,m;
	for(h = 0; h < 16; h++) {
			b = img_vn[pos][h];
				for(m = 7; m >= 0; m--) {
					if (b & 1) {
						draw_pixel( x + m, y + h, c, p_bmp );
						}
						b >>= 1;
					}
				}
}
EXPORT void draw_char(int pos,int x, int y, int c, BMP *p_bmp )
{
	int h,b,m;
	for(h = 0; h < 16; h++) {
			b = img[pos][h];
				for(m = 7; m >= 0; m--) {
					if (b & 1) {
						draw_pixel( x + m, y + h, c, p_bmp );
						}
						b >>= 1;
					}
				}
}
EXPORT void draw_text_vn( char *str, int x, int y, int c, BMP *p_bmp )
{
    int k;
	char *suc_char,*str1;
    for(; *str != '\0'; str++) {
        k = *str - '!';
        if (k < 0 || k >= 94) {
            x += 8; continue;
        }
		switch (k) {
		case 64://a
			suc_char=str+1;
			switch (*suc_char) {
			case 's': //as
				draw_char_vn(0,x,y,c,p_bmp);
				str++;
				break;
			case 'f': //af
				draw_char_vn(1,x,y,c,p_bmp);
				str++;
				break;
			case 'r': //ar
				draw_char_vn(2,x,y,c,p_bmp);
				str++;
				break;
			case 'x': //ax
				draw_char_vn(3,x,y,c,p_bmp);
				str++;
				break;
			case 'j': //aj
				draw_char_vn(4,x,y,c,p_bmp);
				str++;
				break;
			case 'a' ://aa
				str1=str+2;
				switch (*str1) {
				case 's'://aas
					draw_char_vn(6,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'f'://aaf
					draw_char_vn(7,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'r'://aar
					draw_char_vn(8,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'x'://aax
					draw_char_vn(9,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'j'://aaj
					draw_char_vn(10,x,y,c,p_bmp);
					str=str+1;
					break;
				default:
					draw_char_vn(5,x,y,c,p_bmp);
					str++;
					break;
				}//end of aa {s,f,r,x,j}
			case 'w' ://aw
				str1=str+2;
				switch (*str1) {
				case 's'://aws
					draw_char_vn(12,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'f'://awf
					draw_char_vn(13,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'r'://awr
					draw_char_vn(14,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'x'://awx
					draw_char_vn(15,x,y,c,p_bmp);
					str=str+1;
					break;
				case 'j'://awj
					draw_char_vn(16,x,y,c,p_bmp);
					str=str+1;
					break;
				default:
					draw_char_vn(11,x,y,c,p_bmp);
					str++;
					break;
				}//end of aw {s,f,r,x,j}
			default :
				draw_char(k,x,y,c,p_bmp);
			}
		default :
			draw_char(k,x,y,c,p_bmp);
		}
        x += 8;
    }
}
/*
 * draw button
 */
#define RD 4
EXPORT void draw_btn( RECT *r, char *s, int psh,int c, BMP *p_bmp ) {
	int k;
    fill_rect( r -> c.left, r -> c.top, r -> c.right, r -> c.bottom,
           c, p_bmp );

    draw_line( r -> c.left, r -> c.bottom - 1 -RD, r -> c.left, r -> c.top +RD, psh ? BLACK : WHITE, p_bmp );
    draw_line( r -> c.left, r -> c.top +RD, r -> c.left +RD, r -> c.top, psh ? BLACK : WHITE, p_bmp );
    draw_line( r -> c.left +RD, r -> c.top, r -> c.right - 1 -RD, r -> c.top, psh ? BLACK : WHITE, p_bmp );
    draw_line( r -> c.right - 1 -RD, r -> c.top, r -> c.right - 1, r -> c.top +RD, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.right - 1, r -> c.top +RD, r -> c.right - 1, r -> c.bottom - 1 -RD, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.right - 1, r -> c.bottom - 1 -RD, r -> c.right - 1 -RD, r -> c.bottom - 1, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.right - 1 -RD, r -> c.bottom - 1, r -> c.left +RD, r -> c.bottom - 1, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.left +RD, r -> c.bottom - 1, r -> c.left, r -> c.bottom - 1 -RD, psh ? WHITE : BLACK, p_bmp );
    if (c==GRAY) k=BLACK;
    else k= WHITE;
    draw_text( s, (r -> c.left + r -> c.right - 8 * strlen(s)) / 2,
           (r -> c.top + r -> c.bottom - 16) / 2, k, p_bmp );
}

EXPORT void draw_btn1( RECT *r, char *s, int psh,int c, BMP *p_bmp ) {
	int k;
    
    draw_line( r -> c.left, r -> c.bottom - 1 -RD, r -> c.left, r -> c.top +RD, psh ? BLACK : WHITE, p_bmp );
    draw_line( r -> c.left, r -> c.top +RD, r -> c.left +RD, r -> c.top, psh ? BLACK : WHITE, p_bmp );
    draw_line( r -> c.left +RD, r -> c.top, r -> c.right - 1 -RD, r -> c.top, psh ? BLACK : WHITE, p_bmp );
    draw_line( r -> c.right - 1 -RD, r -> c.top, r -> c.right - 1, r -> c.top +RD, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.right - 1, r -> c.top +RD, r -> c.right - 1, r -> c.bottom - 1 -RD, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.right - 1, r -> c.bottom - 1 -RD, r -> c.right - 1 -RD, r -> c.bottom - 1, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.right - 1 -RD, r -> c.bottom - 1, r -> c.left +RD, r -> c.bottom - 1, psh ? WHITE : BLACK, p_bmp );
    draw_line( r -> c.left +RD, r -> c.bottom - 1, r -> c.left, r -> c.bottom - 1 -RD, psh ? WHITE : BLACK, p_bmp );
    if (c==GRAY) k=30;
    else k= WHITE;
    draw_text( s, (r -> c.left + r -> c.right - 8 * strlen(s)) / 2,
           (r -> c.top + r -> c.bottom - 16) / 2, k, p_bmp );
}
/* 256 colors standard color map */
const COLOR colormap[] = {
	0x00ffffff, 0x0000009f, 0x0000ef00, 0x0000efef,
	0x00ff0000, 0x00ef00ff, 0x00dfdf00, 0x007f7f9f,
	0x00dfdfdf, 0x007f9fff, 0x00bfffaf, 0x00cfffff,
	0x00ff6f6f, 0x00ef8fff, 0x00efff9f, 0x00010101,
	0x00ffffcc, 0x00ffff66, 0x00ffff33, 0x00ffff00,
	0x00ffccff, 0x00ffcccc, 0x00ffcc99, 0x00ffcc66,
	0x00ffcc33, 0x00ffcc00, 0x00ff99cc, 0x00ff9999,
	0x00ff9966, 0x00ff9933, 0x00ff9900, 0x00ff66ff,
	0x00ff66cc, 0x00ff6699, 0x00ff6633, 0x00ff6600,
	0x00ff33ff, 0x00ff33cc, 0x00ff3399, 0x00ff3366,
	0x00ff3333, 0x00ff3300, 0x00ff00cc, 0x00ff0099,
	0x00ff0066, 0x00ff0033, 0x00ccff99, 0x00ccff66,
	0x00ccff33, 0x00ccff00, 0x00ccccff, 0x00cccc99,
	0x00cccc66, 0x00cccc33, 0x00cc99ff, 0x00cc99cc,
	0x00cc9999, 0x00cc9966, 0x00cc9933, 0x00cc9900,
	0x00cc66ff, 0x00cc66cc, 0x00cc6699, 0x00cc6666,
	0x00cc6633, 0x00cc6600, 0x00cc33ff, 0x00cc33cc,
	0x00cc3399, 0x00cc3366, 0x00cc3333, 0x00cc3300,
	0x00cc00ff, 0x00cc00cc, 0x00cc0099, 0x00cc0066,
	0x00cc0033, 0x00cc0000, 0x0099ffff, 0x0099ffcc,
	0x0099ff99, 0x0099ff66, 0x0099ff33, 0x0099ff00,
	0x0099ccff, 0x0099cccc, 0x0099cc99, 0x0099cc66,
	0x0099cc33, 0x0099cc00, 0x009999ff, 0x009999cc,
	0x00999999, 0x00999966, 0x00999933, 0x00999900,
	0x009966ff, 0x009966cc, 0x00996699, 0x00996666,
	0x00996633, 0x00996600, 0x009933ff, 0x009933cc,
	0x00993399, 0x00993366, 0x00993333, 0x00993300,
	0x009900ff, 0x009900cc, 0x00990099, 0x00990066,
	0x00990033, 0x00990000, 0x0066ffff, 0x0066ffcc,
	0x0066ff99, 0x0066ff66, 0x0066ff33, 0x0066ff00,
	0x0066ccff, 0x0066cccc, 0x0066cc99, 0x0066cc66,
	0x0066cc33, 0x0066cc00, 0x006699cc, 0x00669999,
	0x00669966, 0x00669933, 0x00669900, 0x006666ff,
	0x006666cc, 0x00666666, 0x00666633, 0x00666600,
	0x006633ff, 0x006633cc, 0x00663399, 0x00663366,
	0x00663333, 0x00663300, 0x006600ff, 0x006600cc,
	0x00660099, 0x00660066, 0x00660033, 0x00660000,
	0x0033ffff, 0x0033ffcc, 0x0033ff99, 0x0033ff66,
	0x0033ff33, 0x0033ff00, 0x0033ccff, 0x0033cccc,
	0x0033cc99, 0x0033cc66, 0x0033cc33, 0x0033cc00,
	0x003399ff, 0x003399cc, 0x00339999, 0x00339966,
	0x00339933, 0x00339900, 0x003366ff, 0x003366cc,
	0x00336699, 0x00336666, 0x00336633, 0x00336600,
	0x003333ff, 0x003333cc, 0x00333399, 0x00333366,
	0x00333333, 0x00333300, 0x003300ff, 0x003300cc,
	0x00330099, 0x00330066, 0x00330033, 0x00330000,
	0x0000ffcc, 0x0000ff99, 0x0000ff66, 0x0000ff33,
	0x0000ff00, 0x0000ccff, 0x0000cccc, 0x0000cc99,
	0x0000cc66, 0x0000cc33, 0x0000cc00, 0x000099ff,
	0x000099cc, 0x00009999, 0x00009966, 0x00009933,
	0x00009900, 0x000066ff, 0x000066cc, 0x00006699,
	0x00006666, 0x00006633, 0x00006600, 0x000033ff,
	0x000033cc, 0x00003399, 0x00003366, 0x00003333,
	0x00003300, 0x000000ff, 0x000000cc, 0x00000066,
	0x00000033, 0x00ee0000, 0x00dd0000, 0x00bb0000,
	0x00aa0000, 0x00880000, 0x00770000, 0x00550000,
	0x00440000, 0x00220000, 0x00110000, 0x0000dd00,
	0x0000bb00, 0x0000aa00, 0x00008800, 0x00007700,
	0x00005500, 0x00004400, 0x00002200, 0x00001100,
	0x000000ee, 0x000000dd, 0x000000bb, 0x000000aa,
	0x00000088, 0x00000077, 0x00000055, 0x00000044,
	0x00000022, 0x00eeeeee, 0x00cccccc, 0x00bbbbbb,
	0x00aaaaaa, 0x00888888, 0x00777777, 0x00555555,
	0x00444444, 0x00222222, 0x00111111, 0x00000000
};
EXPORT void draw( int x, int y, long c, BMP *p_bmp)
{
	c = ((c >> 16) & 0xff) >> 3 << 11 /* red */
          | ((c >> 8) & 0xff) >> 2 << 5   /* edge */
          | (c & 0xff) >> 3;              /* blue */
        ((UH*)((p_bmp -> baseaddr)[0]))[p_bmp -> rowbytes / 2 * y + x] = c;
}
